package cn.jietuo.redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2021/3/4
 * @description: 哨兵高可用测试
 */
public class JedisSentinelsTest {

    public static void main(String[] args) {
        JedisPoolConfig  config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(10);
        config.setMinIdle(5);

        String masterName = "MyMaster";
        Set<String> sentinels = new HashSet<String>();
        sentinels.add(new HostAndPort("192.168.1.100",26379).toString());
        sentinels.add(new HostAndPort("192.168.1.100",26380).toString());
        sentinels.add(new HostAndPort("192.168.1.100",26381).toString());
        // jedisSentinelPool 其实本质上跟jedisPool类似，都是与Redis主节点建立的连接池
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName,sentinels,config,2000,null);
        Jedis jedis = jedisSentinelPool.getResource();
        System.out.println(jedis.set("sentinel","jietuo"));
        System.out.println(jedis.get("sentinel"));
    }
}
